<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
        <!-- jQuery 2.0.2 -->
        <script src="${initParam.root }/static/js/jquery.min.js"></script>
        <script src="${initParam.root }/static/js/jquery-migrate-1.2.1.js"></script>
        <!-- Bootstrap -->
        <script src="${initParam.root }/static/js/bootstrap.min.js" type="text/javascript"></script>
        <!-- AdminLTE App -->
        <script src="${initParam.root }/static/js/AdminLTE/app.js" type="text/javascript"></script>
        <!-- Bootstrap WYSIHTML5 -->
        <script src="${initParam.root }/static/js/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js" type="text/javascript"></script>
        <!-- iCheck -->
		<script src="${initParam.root }/static/js/plugins/iCheck/icheck.min.js"></script>
		<!-- validate -->
		<script src="${initParam.root }/static/js/jquery.validate.js"></script>
        <!-- Page script -->
        <script type="text/javascript">
            $(function() {
				$("#roleForm").validate({
					rules:{
						name:{required:true}
					},
					messages:{
						name:{required:"请填写角色名"}
					}
				});
                "use strict";

                //iCheck for checkbox and radio inputs
                $('input[type="checkbox"]').iCheck({
                    checkboxClass: 'icheckbox_minimal-blue',
                    radioClass: 'iradio_minimal-blue'
                });

                //When unchecking the checkbox
                $("#check-all").on('ifUnchecked', function(event) {
                    //Uncheck all checkboxes
                    $("input[type='checkbox']", ".table-mailbox").iCheck("uncheck");
                });
                //When checking the checkbox
                $("#check-all").on('ifChecked', function(event) {
                    //Check all checkboxes
                    $("input[type='checkbox']", ".table-mailbox").iCheck("check");
                });
                //Handle starring for glyphicon and font awesome
                $(".fa-star, .fa-star-o, .glyphicon-star, .glyphicon-star-empty").click(function(e) {
                    e.preventDefault();
                    //detect type
                    var glyph = $(this).hasClass("glyphicon");
                    var fa = $(this).hasClass("fa");

                    //Switch states
                    if (glyph) {
                        $(this).toggleClass("glyphicon-star");
                        $(this).toggleClass("glyphicon-star-empty");
                    }

                    if (fa) {
                        $(this).toggleClass("fa-star");
                        $(this).toggleClass("fa-star-o");
                    }
                });

                //Initialize WYSIHTML5 - text editor
                $("#email_message").wysihtml5();
                
                /* role script */
                
                /*添加按钮—— 显示编辑模态框 */
                $("#role_add").click(function(){
                	$("#roleEdit").modal('show');
                });
                
                /*编辑按钮——显示编辑模态框*/
                $("#roletbody").on("click",".btn_roleedit",function(){
                	$("#roleEdit").modal('show');
                	/*读取某一角色数据*/
                	var id=$(this).attr("data-id")
                	$.ajax({
                		url:"${initParam.root}/rest/role/update",
                		type:"get",
                		data:{roleId:id},
                		dataType:"json",
                		success:function(json){
                			$("#roleTitle").text("编辑角色");
                			$("#rolename").val(json.name);
                			$("#roleId").val(json.id);
                			$("#roleForm").attr("data-flag","update");
                			json.functionAll.map(function(info){
            					console.log(info.code);
            					$("#"+info.code).iCheck('check'); 
            				})
                		}
                	});
                });
                
                /* 保存按钮 */
                $("#role-save").click(function(){
                	if(!$("#roleForm").valid()){
                		return;	
                	}
                	
                	var info=$("#roleForm").serialize();
                	var path="";
                	var roleId=$("#roleId").val();
                	if(roleId!=null&&roleId!=""){
                		path="${initParam.root}/rest/role/update"
                	}else{
                		path="${initParam.root}/rest/role/add"
                	}
            		$.ajax({
            			url:path,
            			type:"post",
            			dataType:"json",
            			data:info,
            			success:function(json){
            				if(json.isSuccess==true){
            					window.location.reload();
            					$("#roleEdit").modal('hide');
            					$("#roleTitle").text("添加角色");
                            	$("#roleForm").attr("data-flag","");
                            	$("#roleId").val("");
                            	$("#rolename").val("");
                            	$('.role-info').iCheck('uncheck');
            				}
            			}
            		});
                });
                
                
                /*关闭按钮*/
                $("#role-close,#btn_roleClose").click(function(){
                	$("#roleTitle").text("添加角色");
                	$("#roleForm").attr("data-flag","");
                	$("#rolename").val("");
                	$("#roleId").val("");
                	$('.role-info').iCheck('uncheck');
                });
            });
            
            function loadRole(){
            	$.ajax({
            		url:"${initParam.root}/role",
            		type:"get",
            		dataType:"json",
            		success:function(json){
            			if(json.roleList!=null){
            				$("#roletbody").empty();
            				json.roleList.map(function(info){
		            			var line='<tr><td>'+info.name+'</td><td>'+
										'<button class="btn btn-primary btn_roleedit" data-id="'+info.id+'">编辑</button></td></tr>';
								$("#roletbody").append(line);
		    				})
            			}
            		}
            	});
            }
        </script>